﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using System.Web;
using System.Web.Script.Serialization;
using System.IO;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;

namespace bsyc2c
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string str = "\\w+$";
            string user = txtUser.Text.Trim();
            string password = txtPwd.Text.Trim();
            string message;
            string status;
            string url;
            string getJson;
            string[] values;

            try 
            {
                log4net.Config.XmlConfigurator.Configure();
                log4net.ILog log = log4net.LogManager.GetLogger("loginfo");                      // 获取一个日志记录器

                if (user.Length == 0 || password.Length == 0)
                {
                    MessageBox.Show("请输入账号或密码！");
                    return;
                }
                if (!Regex.IsMatch(user, str))
                {
                    MessageBox.Show("账号只能是字母或数字");
                    return;
                }
                else if (!Regex.IsMatch(password, str))
                {
                    MessageBox.Show("密码只能是字母和数字");
                    return;
                }
                else {
                    // 我们的接口
                    url = "http://apitest.bsprint.com/api/Pclogin?mobile_phone=" + user + "&password=" + password;
                    getJson = WebHelper.Get(url);
                    JObject jo = JObject.Parse(getJson);
                    values = jo.Properties().Select(item => item.Value.ToString()).ToArray();
                    status = values[1];    
                    message = values[3];   
                    if (status == "1")
                    {
                        this.Hide();
                        FrmScan FormScan = new FrmScan();
                        FormScan.Show();
                    }
                    else
                    {
                        log.Info(DateTime.Now.ToString() + ": loginfo" + message);                 // 写入一条新log
                        MessageBox.Show(message);
                    }

                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误消息");
            }

        }
    }
}
